package com.dmcb.trade.business.crawlers.author;

import com.dmcb.common.business.services.BaseService;
import com.dmcb.common.business.utils.JsonUtil;
import com.dmcb.common.business.utils.ListUtil;
import com.dmcb.common.business.utils.WebUtil;
import com.dmcb.common.web.conversion.JsonResult;
import com.dmcb.trade.controller.ArticleCrawlerController;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by wangyong on 2017/6/7.
 */
@Service
public class SSPaiCrawler extends BaseService {

    @Autowired
    private ArticleCrawlerController articleCrawlerController;
    org.slf4j.Logger logger = LoggerFactory.getLogger(SSPaiCrawler.class);



    public JsonResult crawl() {
        String[] types = new String[]{"效率工具","手机摄影","生活方式","游戏","硬件","人物","last"};
        int ero = 0;
        int success = 0;
        int total;
        String link;
        for(String type : types){
            int start = 0;
            boolean isEnd = false;
            while (!isEnd) {
                if(type.equals("last")){
                    link = "https://sspai.com/api/v1/articles?offset="+start+"&limit=20&is_matrix=1&sort=matrix_at";
                }else{
                    link = "https://sspai.com/api/v1/articles?offset="+start+"&limit=20&tag="+ WebUtil.URLEncode(type);
                }
                String json = WebUtil.buildRequest(link,false);
                if (StringUtils.isBlank(json)) {
                    return error("解析失败，链接："+link);
                }
                Map<String,Object> map = JsonUtil.getResult(json);
                if(map == null || map.size() == 0){
                    return error("解析失败，链接："+link);
                }
                //已经到达最后一页
                total = (Integer)map.get("total");
                List<Map<String,Object>> mapList =(List) map.get("list");
                if(ListUtil.isEmpty(mapList) || mapList.size() == 0){
                    break;
                }
                for (Map map1 : mapList) {
                    String src = "https://sspai.com/post/"+map1.get("id");
                    if (StringUtils.isBlank(link)) {
                        continue;
                    }
                    JsonResult jsonResult = articleCrawlerController.crawl(3889, src);
                    if (jsonResult.getStatus() != JsonResult.STATUS_SUCCESS) {
                        if (jsonResult.getStatus() == JsonResult.STATUS_ERROR) {
                            ero++;
                        }
                        if (ero >= 10) {
                            return error("连续10次导入失败，请核实原因后再导入");
                        }
                        continue;
                    }
                    ero = 0;
                    success++;
                }
                if(start>total){
                    isEnd = true;
                }
                start+=20;
            }
        }

        return success("成功添加" + success + "条文章");
    }

}
